package com.you.atlas.web.member.module.action;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.service.form.Group;
import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.Navigator;
import com.alibaba.citrus.turbine.TurbineRunData;
import com.alibaba.citrus.turbine.dataresolver.FormGroup;
import com.you.atlas.biz.member.constant.MemberRegisteFailture;
import com.you.atlas.biz.member.constant.MemberRegisteVO;
import com.you.atlas.biz.member.service.MemberService;
import com.you.atlas.web.member.constant.AuthConstant;
import com.you.atlas.web.member.sectury.AuthCallBack;
import com.you.atlas.web.member.sectury.Authenticator;
import com.you.common.result.ServiceResult;
import com.you.common.webx.AuthContext;
import com.you.member.dal.dao.dataobject.MemberDO;

public class RegisteAction implements AuthCallBack {

    @Autowired
    private MemberService memberService;

    @Resource(name = "greenAuthenticator")
    private Authenticator greenAuthenticator;

    public void doRegiste(TurbineRunData rundata, @FormGroup("registe") Group group, Context context,
                          Navigator navigator) {
        MemberRegisteVO memberRegisteVO = new MemberRegisteVO();
        group.setProperties(memberRegisteVO);
        ServiceResult<MemberRegisteFailture> result = memberService.registe(memberRegisteVO);

        if (result.isSuccess()) {
            MemberDO memberDO = (MemberDO) result.getModule();
            rundata.getRequest().setAttribute(AuthConstant.MEMBER_ID, memberDO.getId());
            greenAuthenticator.auth(rundata, navigator, this);
        } else {
            context.put("result", result);
            navigator.forwardTo("registe.vm");
        }

    }

    @Override
    public void failure(TurbineRunData rundata, Navigator navigator) {

    }

    @Override
    public void success(TurbineRunData rundata, Navigator navigator) {

        navigator.redirectTo("memberModule").withTarget("me.vm");
    }
}
